import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/config.dart';
import 'package:yp/entity/product_entity.dart';
import 'package:yp/services/api.dart';

import 'share_state.dart';


class ShareLogic extends GetxController with SingleGetTickerProviderMixin {
  final state = ShareState();

  @override
  void onInit() {
    super.onInit();
    state.tabController = TabController(length: 2, vsync: this);
    state.tabController.index = 0;
  }

  @override
  void onReady() {
    super.onReady();
    state.userId = SpUtil.getInt('empeNumId');
    update();
  }

  void showQrcode(ProductEntity item){
    print(item);
    var data;
    if(item.isPreSale==1){
      data =  "{\"currentShopId\":${item.ecShopNumId},\"mberType\":2,\"applyerId\":${state.userId},\"otherguide\":false,\"isGopreSale\":1,\"style_num_id\":${item.styleNumId},\"itemid\":${item.itemNumId}}";
    }else{
      data =  "{\"currentShopId\":${item.ecShopNumId},\"mberType\":2,\"applyerId\":${state.userId},\"otherguide\":false,\"isGoGoodDetail\":true,\"style_num_id\":${item.styleNumId},\"itemid\":${item.itemNumId}}";
    }
    var params = {
      "qrcode_media_type": 2,
      "qrcode_type": 0,
      "content": null,
      "miniprogram_code": {
        "appid": Config.MINI_APPID,
        "scene": "",
        "page": 'pages/index'
      },
      "related_entity_id": 3961529,
      "valid_type": 1,
      "valid_length": null,
      "datajson":data,
      "recreate_sign": 0
    };
    // EasyLoading.show(status: '加载中...');

    Api.kocQrcode(params).listen((event) {
      if (event['code'] == 0) {
        state.qrCode = event['qrcode']['qrcode_url'] ?? '';
      }
      update();
      EasyLoading.dismiss();
    }).onError((e){
      EasyLoading.dismiss();
    });
    update();
  }

  void dispose() {
    update();
  }
}
